package com.shawn.spring.boot.mybatis.service.impl;

import com.shawn.spring.boot.mybatis.entity.Menu;
import com.shawn.spring.boot.mybatis.entity.MenuExample;
import com.shawn.spring.boot.mybatis.mapper.MenuMapper;
import com.shawn.spring.boot.mybatis.service.MenuService;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author shawn
 * @since 2020/8/30
 */
@Service
public class MenuServiceImpl implements MenuService {
  @Autowired private MenuMapper menuMapper;

  @Override
  public void add(Menu menu) {
    Date now = Calendar.getInstance().getTime();
    menu.setCreateTime(now);
    menu.setUpdateTime(now);
    menu.setIsDeleted(0);
    menuMapper.insert(menu);
  }

  @Override
  public List<Menu> findByPid(long pid) {
    MenuExample example = new MenuExample();
    example.createCriteria().andParentIdEqualTo(pid);
    return menuMapper.selectByExample(example);
  }

  @Override
  public Menu findById(long id) {
    return menuMapper.selectByPrimaryKey(id);
  }

  @Override
  public List<Menu> findByRoleId(long roleId) {
    return menuMapper.selectByRoleId(roleId);
  }
}
